package com.unionfetch.content_home.ui.view

import android.animation.ValueAnimator
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.animation.addListener
import com.unionfetch.content_home.R
import com.unionfetch.content_home.bean.TradeDataDetail
import com.unionftech.common.R.string
import com.unionftech.common.common.language.DynamicResourceManager
import com.unionftech.common.utils.Utils

class TradeAccountDetailCardView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0,
) : LinearLayout(context, attrs, defStyleAttr) {
    private val mTvBalance by lazy { findViewById<TextView>(R.id.tv_balance) }
    private val mTvCredit by lazy { findViewById<TextView>(R.id.tv_credit) }
    private val mTvBalanceLabel by lazy { findViewById<TextView>(R.id.tv_balance_label) }
    private val mTvCreditLabel by lazy { findViewById<TextView>(R.id.tv_credit_label) }
    private val mTvFree by lazy { findViewById<TextView>(R.id.tv_free) }
    private val mTvMargin by lazy { findViewById<TextView>(R.id.tv_margin) }
    private val mTvFreeLabel by lazy { findViewById<TextView>(R.id.tv_free_label) }
    private val mTvMarginLabel by lazy { findViewById<TextView>(R.id.tv_margin_label) }
    private val mLlExpandContainer by lazy { findViewById<LinearLayout>(R.id.ll_expand_container) }
    private val mTvFloatSurplus by lazy { findViewById<TextView>(R.id.tv_float_surplus) }
    private val mTvMarginLevel by lazy { findViewById<TextView>(R.id.tv_margin_level) }
    private val mTvFloatSurplusLabel by lazy { findViewById<TextView>(R.id.tv_float_surplus_label) }
    private val mTvMarginLevelLabel by lazy { findViewById<TextView>(R.id.tv_margin_level_label) }
    private val mTvEquityUnit by lazy { findViewById<TextView>(R.id.tv_equity_unit) }
    private val mTvEquity by lazy { findViewById<TextView>(R.id.tv_equity) }
    private val mIvExpand by lazy { findViewById<ImageView>(R.id.iv_expand) }
    private val mTvEquityLabel by lazy { findViewById<TextView>(R.id.tv_equity_label) }
    private var mContext = context
    private var mIsExpand = false

    init {
        LayoutInflater.from(mContext)
            .inflate(R.layout.layout_wv_trade_account_detail_view, this, true)
        initLanguageTextForLabel()
        setListener()
    }

    private fun initLanguageTextForLabel() {
        mTvEquityLabel.text = DynamicResourceManager.getString(mContext, string.equity)
        mTvMarginLevelLabel.text = DynamicResourceManager.getString(mContext, string.margin_level)
        mTvFloatSurplusLabel.text =
            DynamicResourceManager.getString(mContext, string.float_surplus)
        mTvMarginLabel.text = DynamicResourceManager.getString(mContext, string.margin)
        mTvFreeLabel.text = DynamicResourceManager.getString(mContext, string.free)
        mTvCreditLabel.text = DynamicResourceManager.getString(mContext, string.credit)
        mTvBalanceLabel.text = DynamicResourceManager.getString(mContext, string.balance)
    }

    private fun setListener() {
        mIvExpand.setOnClickListener {
            if (!Utils.isFastDoubleClick(it.id)) {
                actionExpandOrHideAnim()
            }
        }
    }

    private fun actionExpandOrHideAnim() {
        if (!mIsExpand) {
            mLlExpandContainer.visibility = VISIBLE
            mLlExpandContainer.measure(0, 0)
        }
        val layoutParams = mLlExpandContainer.layoutParams
        val animator: ValueAnimator = if (mIsExpand) {
            ValueAnimator.ofInt(mLlExpandContainer.measuredHeight, 0)
        } else {
            ValueAnimator.ofInt(0, mLlExpandContainer.measuredHeight)
        }
        animator.addUpdateListener { animation ->
            layoutParams.height = (animation.animatedValue as Int)
            mLlExpandContainer.requestLayout()
        }
        animator.addListener(onEnd = {
            if (mIsExpand) {
                mLlExpandContainer.visibility = GONE
                mIvExpand.setImageResource(R.drawable.ic_arrow_expand)
            } else {
                mLlExpandContainer.visibility = VISIBLE
                mIvExpand.setImageResource(R.drawable.ic_arrow_hide)
            }
            mIsExpand = !mIsExpand
        })
        animator.duration = 300
        animator.start()
    }

    /**
     * 更新净值等数据
     */
    fun updateData(tradeDataDetail: TradeDataDetail) {
        mTvEquity.text = tradeDataDetail.equity.toString()
        mTvEquityUnit.text = tradeDataDetail.currency
        mTvMarginLevel.text = tradeDataDetail.marginLevel.toString()
        mTvMargin.text = tradeDataDetail.margin.toString()
        mTvFree.text = tradeDataDetail.marginFree.toString()
        mTvCredit.text = tradeDataDetail.credit.toString()
        mTvBalance.text = tradeDataDetail.balance.toString()
        //将浮动盈亏先设置成-
        mTvFloatSurplus.text = "-"
    }

    /**
     * 更新浮动盈余
     */
    fun updateFloatSurplus(floatSurplus: String) {
        mTvFloatSurplus.text = floatSurplus
    }
}